<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <title>传智健康</title>
    <script src="../js/vue.js"></script>
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
</head>
<body>
<h1>修改密码</h1>
<div id="app">
    <el-steps :active="active" finish-status="success">
        <el-step title="修改密码"></el-step>
        <el-step title="完成"></el-step>
    </el-steps>
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="新密码" prop="pass">
            <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
            <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm()">提交</el-button>
            <el-button @click="resetForm()">重置</el-button>
        </el-form-item>
    </el-form>
</div>
<script>

    var Main = {
        data() {
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.ruleForm.pass) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                ruleForm: {
                    pass: '',
                    checkPass: ''
                },
                rules: {
                    pass: [
                        {validator: validatePass, trigger: 'blur'}
                    ],
                    checkPass: [
                        {validator: validatePass2, trigger: 'blur'}
                    ]
                },
                active: 1
            };
        },
        methods: {
            submitForm() {
                // 修改密码

                //表单验证发送请求
                this.$refs['ruleForm'].validate((valid) => {
                    if (valid) {
                        // alert('submit!');
                        // 弹出确认框
                        this.$confirm('此操作将修改您的密码, 是否继续?', '提示', {
                            confirmButtonText: '确定',
                            cancelButtonText: '取消',
                            type: 'warning'
                        }).then(() => {
                            // 发送请求修改密码
                            axios.post("/user/chengedByUsername.do?password="+this.ruleForm.pass).then((res) => {
                                if (res.data.flag) {
                                    //提示修改成功
                                    this.$message.success(res.data.message);
                                    //步骤条跳转
                                    if (this.active++ > 1) this.active = 0;
                                } else {
                                    //提示错误信息
                                    this.$message.error(res.data.message);
                                }
                            })
                        }).catch((error) => {
                            console.log(error);
                            this.$message({
                                type: 'info',
                                message: '已取消修改'
                            });
                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                })
                // this.$message({
                //     type: 'success',
                //     message: '删除成功!'
                // });
            },
            resetForm() {
                this.$refs['ruleForm'].resetFields();
            }
        }
    }
    var Ctor = Vue.extend(Main)
    new Ctor().$mount('#app')
</script>
</body>
</html>
